package de.iritgo.skillfull.component.visitor;

import de.iritgo.skillfull.component.CollisionComponent;
import de.iritgo.skillfull.component.CollisionComponentImpl;
import de.iritgo.skillfull.component.motion.MoveFieldComponent;
import de.iritgo.skillfull.component.motion.MoveRotationComponent;
import de.iritgo.skillfull.component.motion.PositionComponent;
import de.iritgo.skillfull.component.visual.AnimationRenderComponent;
import de.iritgo.skillfull.component.visual.ImageRenderComponent;
import de.iritgo.skillfull.component.visual.SimpleTextComponent;
import de.iritgo.skillfull.component.visual.SpriteSheetRenderComponent;
import de.iritgo.skillfull.component.visual.VirusComponent;
import de.iritgo.skillfull.entity.Entity;

/**
 * Set a given position on the components
 * 
 * @author held
 *
 */
public class PositionVisitor extends BaseVisitor
{
	int x,y;
	private boolean relative;
	
	public PositionVisitor (int x, int y, boolean relative)
	{
		this.x = x;
		this.y = y;
		this.relative = relative;
	}
	
	@Override
	public void visit(Entity entity) 
	{
		super.visit(entity);
		entity.setPosition(x, y);
	}

	public void visit(VirusComponent virusComponent) 
	{
		virusComponent.setX (x);
		virusComponent.setY (y);
	}

	public void visit (CollisionComponent collisionComponent) 
	{
		if (! relative)
		{
			collisionComponent.setX (x);
			collisionComponent.setY (y);
			collisionComponent.registerOnCollisionManager ();
			collisionComponent.moved ();
			collisionComponent.setActive (true);
		}
	}
	
	public void visit (MoveFieldComponent moveFieldComponent) 
	{
		if (! moveFieldComponent.isRelative ())
		{
			if (relative)
			{
				x += moveFieldComponent.getX ();
				y += moveFieldComponent.getY ();
			}
			else
			{
				moveFieldComponent.setX (x);	
				moveFieldComponent.setY (y);
			}
		}
	}

	public void visit(MoveRotationComponent moveRotationComponent) 
	{
		if (! relative)
		{
			moveRotationComponent.setX (x);
			moveRotationComponent.setY (y);
		}
	}
	
	public void visit (PositionComponent positionComponent)	
	{	
		if (! positionComponent.isRelative ())
		{
			if (relative)
			{
				x += positionComponent.getX ();
				y += positionComponent.getY ();
			}
			positionComponent.setX (x);
			positionComponent.setY (y);
		}
		else
		{
			x += positionComponent.getX ();
			y += positionComponent.getY ();
		}
	}

	public void visit (SimpleTextComponent simpleTextComponent) 
	{
		simpleTextComponent.setX (x);
		simpleTextComponent.setY (y);
	}
	
	public void visit (ImageRenderComponent renderComponent)
	{
		renderComponent.setX (x);
		renderComponent.setY (y);
	}

	public void visit (AnimationRenderComponent animationRenderComponent)
	{
		animationRenderComponent.setX (x);
		animationRenderComponent.setY (y);
	}

	public void visit(SpriteSheetRenderComponent spriteSheetRenderComponent) 
	{ 
		spriteSheetRenderComponent.setX (x);
		spriteSheetRenderComponent.setY (y);
	}
}
